﻿//MetaDataCollections
//DataSourceInformation
//DataTypes
//Restrictions
//ReservedWords
//Users
//Databases
//Tables
//Columns
//StructuredTypeMembers
//Views
//ViewColumns
//ProcedureParameters
//Procedures
//ForeignKeys
//IndexColumns
//Indexes
//UserDefinedTypes

using System;
using System.Data;
using System.Windows.Forms;

using Word;

public partial class sql分析 : Form
{
    WordApp CurWordApp;

    public sql分析()
    {
        InitializeComponent();
    }

    //*********************************************************
    //测试
    //*********************************************************
    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.textBox1.Text == "") { MessageBox.Show("非空"); return; }
            if (this.textBox2.Text == "") { MessageBox.Show("非空"); return; }

            string datasource = this.textBox1.Text;
            string database = this.textBox2.Text;

            Tsql.boolSql = true;
            Tsql.database = database;
            Tsql.datasource = datasource;

            Tsql t = new Tsql();
            if (t.TestSqlConnstring())
            {
                MessageBox.Show("成功！");
            }
            else
            {
                MessageBox.Show("失败！");
            }

            this.dataGridView1.DataSource = t.getNameSql();
        }
        catch (Exception ex)
        {
            Log.Console.WriteLine("Error=============================");
            Log.Console.WriteLine("Message:" + ex.Message);
            Log.Console.WriteLine("ENDError=============================");
        }

        //DataTable dt = t.GetDataSourceSql("select attach_document_name as 附件名称, attach_path as 内容, attach_id as 编号, Attach_Date as 上传时间, Construct_Course_Type as 文档分类, Construct_Course_Name as 文档所在目录, Attach_Name as 文档标题, Distinction as 级别, Attach_Memo as 文档说明  from [附件] ");
    }

    //*********************************************************
    //文档
    //*********************************************************
    public struct Style
    {
        //public static string One = "Heading 1";
        //public static string Two = "Heading 2";
        //public static string Tree = "Heading 3";
        //public static string Four = "Heading 4";
        //public static string Five = "Heading 5";

        public static string One = "标题 1";
        public static string Two = "标题 2";
        public static string Tree = "标题 3";
        public static string Four = "标题 4";
        public static string Five = "标题 5";
    }

    public struct Color
    {
        public static WdColor ColorClass = WdColor.wdColorDarkBlue;
        public static WdColor ColorEnum = WdColor.wdColorRose;
        public static WdColor ColorInterface = WdColor.wdColorDarkGreen;
        public static WdColor ColorStruct = WdColor.wdColorLightBlue;
        public static WdColor ColorDelegate = WdColor.wdColorRed;
        public static WdColor ColorEvent = WdColor.wdColorPink;

        public static WdColor ColorFunction = WdColor.wdColorRed;
    }

    //*********************************************************
    // 生成word文档
    //*********************************************************
    private void button2_Click(object sender, EventArgs e)
    {
        if (this.textBox1.Text == "") { MessageBox.Show("非空"); return; }
        if (this.textBox2.Text == "") { MessageBox.Show("非空"); return; }

        string datasource = this.textBox1.Text;
        string database = this.textBox2.Text;
        string tableName;

        createDocument();
        InsertTitle(database, Style.One);

        Tsql.boolSql = true;
        Tsql.database = database;
        Tsql.datasource = datasource;

        Tsql t = new Tsql();

        DataTable dataTable = t.getNameSql();

        foreach (DataRow dr in dataTable.Rows)
        {
            if ((String)dr["TABLE_NAME"] != "")
            {
                InsertTitle((String)dr["TABLE_NAME"], Style.Two, WdColor.wdColorBrown);

                tableName = (String)dr["TABLE_NAME"];
                DataTable dataTable1 = t.GetDataSourceSql("select   a.[name] as lieming,b.[name] as leixing    from   syscolumns a,systypes b  where   a.id=object_id('" + tableName + "') and a.xtype=b.xtype");
                foreach (DataRow dr1 in dataTable1.Rows)
                {
                    InsertTitle(dr1["lieming"] + ":  " + dr1["leixing"], Style.Tree, Color.ColorClass);
                }
            }
        }

        MessageBox.Show("ok");
    }

    private void InsertTitle(string sourse, string Format)
    {
        CurWordApp.insertTitle(sourse, Format);
    }

    private void InsertTitle(string sourse, string Format, WdColor wdColor)
    {
        CurWordApp.insertTitle(sourse, Format, wdColor);
    }

    private void createDocument()
    {
        WordApp wordApp = new WordApp();
        CurWordApp = wordApp;
        wordApp.createDocument();
    }

    private void sql分析_Load(object sender, EventArgs e)
    {
    }
}